package com.core.zyyz.amc.modules.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.fastjson.util.TypeUtils;
import com.core.zyyz.amc.modules.mapper.AmcIndexConfigBMapper;
import com.core.zyyz.pcmc.domain.PcmcRole;
import com.core.zyyz.pcmc.domain.PcmcUser;
import com.core.zyyz.pcmc.mapper.PcmcRoleUserMapper;
import com.core.zyyz.pcmc.service.PcmcRoleService;
import org.apache.commons.collections4.MapUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.core.common.annotation.Log;
import com.core.common.core.controller.BaseController;
import com.core.common.core.domain.AjaxResult;
import com.core.common.enums.BusinessType;
import com.core.zyyz.amc.modules.domain.AmcIndexConfigB;
import com.core.zyyz.amc.modules.service.AmcIndexConfigBService;
import com.core.common.core.page.TableDataInfo;

/**
 * 首页配置信息Controller
 *
 * @author LiuFushun
 * @date 2024-10-25
 */
@RestController
@RequestMapping("/amc/indexConfigB")
public class AmcIndexConfigBController extends BaseController {
    @Autowired
    private AmcIndexConfigBService amcIndexConfigBService;
    @Autowired
    private PcmcRoleService pcmcRoleService;
    @Autowired
    private AmcIndexConfigBMapper amcIndexConfigBMapper;
    @Autowired
    private PcmcRoleUserMapper pcmcRoleUserMapper;

    /**
     * 查询首页配置信息列表
     */
    @PreAuthorize("@ss.hasPermi('amc:indexConfigB:list')")
    @GetMapping("/list")
    public TableDataInfo list(AmcIndexConfigB amcIndexConfigB)
    {
        startPage();
        List<AmcIndexConfigB> list = amcIndexConfigBService.selectAmcIndexConfigBList(amcIndexConfigB);
        return getDataTable(list);
    }

    /**
     * 获取首页配置信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('amc:indexConfigB:query')")
    @GetMapping(value = "/{indexId}")
    public AjaxResult getInfo(@PathVariable("indexId") String indexId)
    {
        return success(amcIndexConfigBService.selectAmcIndexConfigBByIndexId(indexId));
    }

    /**
     * 新增首页配置信息
     */
    @PreAuthorize("@ss.hasPermi('amc:indexConfigB:add')")
    @Log(title = "首页配置信息", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody Map<String, Object> paramMap) {
        AmcIndexConfigB amcIndexConfigB = TypeUtils.castToJavaBean(paramMap,AmcIndexConfigB.class);
        //分割所选角色
        String roleCode = MapUtils.getString(paramMap, "roleCode");
        String[] roles = roleCode.split(",");
        List<String> list = new ArrayList<>();
        for (String role : roles) {
            list.add(role);
        }
        //保存信息
        saveMassage(list, amcIndexConfigB);
        return toAjax(1);
    }

    /**
     * 修改首页配置信息
     */
    @PreAuthorize("@ss.hasPermi('amc:indexConfigB:edit')")
    @Log(title = "首页配置信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AmcIndexConfigB amcIndexConfigB)
    {
        return toAjax(amcIndexConfigBService.updateAmcIndexConfigB(amcIndexConfigB));
    }

    /**
     * 删除首页配置信息
     */
    @PreAuthorize("@ss.hasPermi('amc:indexConfigB:remove')")
    @Log(title = "首页配置信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{indexIds}")
    public AjaxResult remove(@PathVariable String[] indexIds)
    {
        return toAjax(amcIndexConfigBService.deleteAmcIndexConfigBByIndexIds(indexIds));
    }

    //保存角色名称与编码
    public AmcIndexConfigB setRoleName(String roleCode, AmcIndexConfigB amcIndexConfigB){
        PcmcRole pcmcRole = pcmcRoleService.selectPcmcRoleByRoleCode(roleCode);
        if(pcmcRole != null){
            amcIndexConfigB.setRoleName(pcmcRole.getRoleName());
            amcIndexConfigB.setRoleCode(roleCode);
            if("1".equals(amcIndexConfigB.getIsShowWork())){
                amcIndexConfigB.setAttributeText1("1");
                amcIndexConfigB.setAttributeText6("6");
                amcIndexConfigB.setAttributeText8("8");
            }
            if("1".equals(amcIndexConfigB.getIsShowRisk())){
                amcIndexConfigB.setAttributeText10("10");
                amcIndexConfigB.setAttributeText12("12");
                amcIndexConfigB.setAttributeText15("15");
            }
        }
        return amcIndexConfigB;
    }

    //保存首页配置信息
    public void saveMassage(List<String> list, AmcIndexConfigB amcIndexConfigB){
        if(null != list && list.size() > 0){
            for(int i = 0; i < list.size(); i++){
                AmcIndexConfigB indexConfigB = setRoleName(list.get(i), amcIndexConfigB);
                List<PcmcUser> userList = pcmcRoleUserMapper.getRoleCodeGrantUserList(list.get(i));
                if(userList.size()>0){
                    userList = userList.stream().distinct().collect(Collectors.toList());
                    for(int j = 0;j < userList.size(); j++){
                        AmcIndexConfigB amcIndexConfigB2 = amcIndexConfigBMapper.selectAmcIndexConfigBUserAndRole(list.get(i),userList.get(j).getUserCode());
                        if(null != amcIndexConfigB2){
                            amcIndexConfigBMapper.deleteAmcIndexConfigBUserAndRole(list.get(i),userList.get(j).getUserCode());
                        }
                        indexConfigB.setEmployeeCode(userList.get(j).getUserCode());
                        indexConfigB.setEmployeeName(userList.get(j).getUserName());
                        indexConfigB.setIndexId(UUID.randomUUID().toString());
                        amcIndexConfigBService.insertAmcIndexConfigB(indexConfigB);
                    }
                }
            }
        }
    }

}
